package com.cysion.study.ui

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.whiteTextTheme
import com.cysion.other.color
import com.cysion.other.startActivityForResult_ex
import com.cysion.other.startActivity_ex
import com.cysion.targetfun._subscribe
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.R
import com.cysion.study.constant.BUNDLE_KEY
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.ui.login.ForgetPwdActivity
import com.cysion.study.ui.personal.SettingActivity
import com.cysion.study.widget.ShowToast
import com.hgj.paydialog.view.PayDialog
import kotlinx.android.synthetic.main.activity_withdraw.*

class WithDrawActivity : BaseActivity(), View.OnClickListener, PayDialog.PayInterface {



    override fun getLayoutId(): Int = R.layout.activity_withdraw

    override fun initView() {
        whiteTextTheme(color(R.color.main_cl))
        topbar.apply {
            setTitle("我的提现")
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }
            }
        }

        tvNext.setOnClickListener(this)
        tv_jilu.setOnClickListener(this)

        tv_alipay.setOnClickListener(this)
        tv_card.setOnClickListener(this)
        tv_all.setOnClickListener(this)
        tv_bank.setOnClickListener(this)
    }

    var money = ""
    var isHasPayPassword = "0"
    var type = "0"//必传 提现类型: 0.支付宝 1.银行卡
    var password = ""

    override fun onResume() {
        super.onResume()
        Alert.loading(self)
        ApiClient.api.withdrawalGetInfo(UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    money = it.money
                    tv_money.text = "当前余额 " + money
                    ////是否已有支付密码 0.无 1.有
                    isHasPayPassword = it.isHasPayPassword
                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_all -> {//全部提现
                et_money.setTextEx(money)

            }
            R.id.tv_bank -> {//选择银行卡
//                startActivity_ex<BandCardActivity>()//BankListActivity
                startActivityForResult_ex<BandCardActivity>(301)
            }
            R.id.tvNext -> {
                //是否已有支付密码 0.无 1.有
                if (isHasPayPassword == "0") {
                    startActivity_ex<SettingActivity>()
                } else {
                    withOutMoney()
                }

            }
            R.id.tv_jilu -> {
                startActivity_ex<WithDrawRecordActivity>()
            }
            R.id.tv_alipay -> {
                tv_alipay.setBackgroundResource(R.drawable.blue_bg)
                tv_card.setBackgroundResource(R.drawable.border_gray_bg)
                tv_pay_type.setText("支付宝账号")
                tv_alipay.setTextColor(resources.getColor(R.color.white))
                tv_card.setTextColor(resources.getColor(R.color.cl999))
                et_card.visibility = View.VISIBLE
                tv_bank.visibility = View.GONE
                type = "0"
            }
            R.id.tv_card -> {
                tv_alipay.setBackgroundResource(R.drawable.border_gray_bg)
                tv_card.setBackgroundResource(R.drawable.blue_bg)
                tv_pay_type.setText("银行卡号")
                tv_alipay.setTextColor(resources.getColor(R.color.cl999))
                tv_card.setTextColor(resources.getColor(R.color.white))
                et_card.visibility = View.GONE
                tv_bank.visibility = View.VISIBLE
                type = "1"
            }
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 301) {
            if (resultCode == Activity.RESULT_OK) {
                bid = data?.getBundleExtra("key")?.getString("bid").toString()
                tv_bank.text = data?.getBundleExtra("key")?.getString("bankCard").toString()

            }
        }
    }
    private var bid = ""
    private var alipay = ""
    private lateinit var payDialog: PayDialog

    private fun withOutMoney() {
        if (TextUtils.isEmpty(et_money.textEx.trim())) {
            ShowToast("请输入提现金额")
            return
        }
         alipay = et_card.textEx.trim()
//        var bid = tv_bank.text.toString().trim()
        //0.支付宝 1.银行卡
        if (type == "0") {
            bid = ""
            if (TextUtils.isEmpty(alipay)) {
                ShowToast("请输入支付宝账号")
                return
            }
        } else if (type == "1") {
            if (TextUtils.isEmpty(bid)) {
                ShowToast("请选择银行卡号")
                return
            }
            alipay = ""
        }

        payDialog = PayDialog(this@WithDrawActivity, "提现金额：" + et_money.text.toString().trim() + "元", this@WithDrawActivity)
        payDialog.show()
    }


    //以下3个方法为输入支付密码弹窗UI回调
    //start
    override fun Payfinish(password: String?) {

        Alert.loading(self)
        ApiClient.api.withdrawalApply(UserCache.token , et_money.textEx.trim())
            .compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    ShowToast("提现申请已提交")
                    payDialog.setSucc()
                    finish()
                }
                _onError {
                    Alert.close()
                    payDialog.setError(ErrorHandler.handle(it).errorMsg)
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }
    override fun onSucc() {
        //回调 成功，关闭dialog 做自己的操作
        payDialog.cancel()
    }

    override fun onForget() {
        //当progress显示时，说明在请求网络，这时点击忘记密码不作处理
        if (payDialog.payPassView.progress.visibility != View.VISIBLE) {
            ShowToast("去找回支付密码")
            //pageType页面共用类型  1修改登录密码  2设置支付密码  0忘记密码
            val pageType = "2"
            var bundle = Bundle()
            bundle.putString("pageType", pageType)
            self.startActivity_ex<ForgetPwdActivity>(BUNDLE_KEY,bundle)
            payDialog.cancel()
        }
    }
    //end

    override fun closeMvp() {
    }

}
